package com.zhhtech.zhh.ui.activity.main

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.view.View
import android.view.animation.LinearInterpolator
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import com.kingja.loadsir.core.LoadService
import com.lxj.xpopup.XPopup
import com.zhhtech.zhh.api.api
import com.zhhtech.zhh.api.core.FirmException
import com.zhhtech.zhh.api.core.launchUI
import com.zhhtech.zhh.api.core.toData
import com.zhhtech.zhh.bean.GetConfig
import com.zhhtech.zhh.common.checkLogin
import com.zhhtech.zhh.databinding.ActivitySchemeSubmitBinding
import com.zhhtech.zhh.databinding.ItemCheckBinding
import com.zhhtech.zhh.ui.api.ApiObserver
import com.zhhtech.zhh.ui.base.BaseActivity
import com.zhhtech.zhh.ui.base.BaseSignCheckAdapter
import com.zhhtech.zhh.ui.dialog.SchemeResultDialog
import com.zhhtech.zhh.ui.ext.toastMessageLong
import com.zhhtech.zhh.ui.ext.value
import com.zhhtech.zhh.ui.helper.jumpWx
import com.zhhtech.zhh.ui.helper.loadsir.LoadSirHelper
import com.zhhtech.zhh.ui.helper.softAdapter
import com.zhhtech.zhh.ui.utils.FullyGridLayoutManager

class SchemeSubmitActivity : BaseActivity<ActivitySchemeSubmitBinding>() {
    private lateinit var mLoadService: LoadService<*>
    private val mLoanTypeAdapter = SchemeSubmitCheckAdapter()
    private val mMoneyTypeAdapter = SchemeSubmitCheckAdapter()
    override fun initView() {
        mLoadService = LoadSirHelper.register(vb.loadview, listener = ::onReload)
        bindViewClick(vb.buttonSubmit)
        vb.rvLoanType.layoutManager = FullyGridLayoutManager(mThis, 2)
        vb.rvMoneyType.layoutManager = FullyGridLayoutManager(mThis, 2)
        softAdapter(vb.llInput, vb.sv)
        onReload()
        val scaleAnimationSet = AnimatorSet()
        val scaleY = ObjectAnimator.ofFloat(vb.buttonSubmit, "scaleY", 1f, 0.9f, 1f)
        val scaleX = ObjectAnimator.ofFloat(vb.buttonSubmit, "scaleX", 1f, 0.9f, 1f)
        scaleY.repeatCount = ValueAnimator.INFINITE
        scaleX.repeatCount = ValueAnimator.INFINITE
        scaleAnimationSet.interpolator = LinearInterpolator()
        scaleAnimationSet.playTogether(scaleX, scaleY)
        scaleAnimationSet.duration = 2000
        scaleAnimationSet.start()
        lifecycle.addObserver(object : DefaultLifecycleObserver {
            override fun onDestroy(owner: LifecycleOwner) {
                super.onDestroy(owner)
                scaleAnimationSet.end()
                scaleAnimationSet.cancel()
            }
        })
    }

    private fun onReload() {
        launchUI({
            val caseConfig = api.indexConfig().toData()
            mLoanTypeAdapter.datas = (caseConfig?.newLoadType ?: mutableListOf()).apply {
                if (isNotEmpty()) {
                    get(0).check = true
                }
            }
            vb.rvLoanType.adapter = mLoanTypeAdapter
            mMoneyTypeAdapter.datas = (caseConfig?.moneyType ?: mutableListOf()).apply {
                if (isNotEmpty()) {
                    get(0).check = true
                }
            }
            vb.rvMoneyType.adapter = mMoneyTypeAdapter
        }, ApiObserver(mThis, loadService = mLoadService))
    }

    override fun onClick(v: View?) {
        when (v) {
            vb.buttonSubmit -> checkLogin {
                launchUI({
                    val data = api.orderCreate(
                        mLoanTypeAdapter.getCheck().id,
                        mMoneyTypeAdapter.getCheck().id,
                        vb.etUsername.value
                    ).toData() ?: throw FirmException()
                    val config = api.indexConfig().toData()
                    if (config?.jump?.type == 3) {
                        XPopup.Builder(mThis).isDestroyOnDismiss(true)
                            .asCustom(SchemeResultDialog(mThis, data))
                            .show()
                    } else {
                        jumpWx(config)
                    }
                }, ApiObserver(mThis) {
                    "提交成功".toastMessageLong()
                })
            }
        }
    }
}

class SchemeSubmitCheckAdapter : BaseSignCheckAdapter<ItemCheckBinding, GetConfig>() {
    override fun onBindViewHolder(vb: ItemCheckBinding, bean: GetConfig, position: Int) {
        vb.rbType.isChecked = bean.check
        vb.rbType.text = bean.name
        setViewClick(position, vb.rbType)
    }
}